<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class SystemAdmin extends Model
{
    protected $pk = "admin_id";

    /**
     * 获取管理员信息
     * @param $account
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getAdminInfoByAccount($account){
        $where = [
            'is_del'=>0,
            'account'=>$account,
        ];
        return $this->where($where)->find();
    }

    /**
     * 修改内容
     * @param $adminId
     * @param $saveData
     * @return static
     */
    function updateInfo($adminId,$saveData){
        return $this->where('admin_id',$adminId)->update($saveData);
    }

    /**
     * 返回管理员信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getAdminInfoById($id){
        return $this->where('admin_id',$id)->find();
    }

    /**
     * 管理员列表
     * @param $getData
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getAdminList($getData,$page,$limit){
        $where = ['is_del'=>0];
        $sql = $this->where($where);
        if(isset($getData['account']) && $getData['account']) $sql = $sql->whereLike('account','%'.$getData['account'].'%');
        if(isset($getData['real_name']) && $getData['real_name']) $sql = $sql->whereLike('account','%'.$getData['real_name'].'%');
        $count = $sql->count();
        $field = 'admin_id,account,real_name,status,roles,login_count,create_time';
        $statusType = [0=>'冻结',1=>'正常'];
        $systemRoleModel = new SystemRole();
        $data = $sql->page($page,$limit)->field($field)->order('admin_id','desc')->select()->each(function ($item)use($statusType,$systemRoleModel){
            $item['status_name'] = $statusType[$item['status']];
            $roleName = $systemRoleModel->whereIn('role_id',$item['roles'])->column('role_name');
            $item['role_name'] = implode(",",$roleName);
            return $item;
        })->toArray();
        return compact('count','data');
    }

    /**
     * 添加管理员
     * @param $addData
     * @return int|string
     */
    function addData($addData){
        return $this->insertGetId([
            'account'=>$addData['account'],
            'pwd'=>password_hash($addData['password'], PASSWORD_BCRYPT),
            'real_name'=>$addData['real_name'],
            'roles'=>implode(",",$addData['role_id']),
            'last_ip'=>'',
            'login_count'=>0,
            'status'=>$addData['status'],
            'is_del'=>0,
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
        ]);
    }

    /**
     * 修改管理员
     * @param $saveData
     * @return int|string
     */
    function editData($saveData){
        $updateData = [
            'account'=>$saveData['account'],
            'real_name'=>$saveData['real_name'],
            'roles'=>implode(",",$saveData['role_id']),
            'status'=>$saveData['status'],
            'update_time'=>date("Y-m-d H:i:s"),
        ];
        if($saveData['password']) $updateData['pwd'] = password_hash($saveData['password'], PASSWORD_BCRYPT);
        return $this->where('admin_id',$saveData['admin_id'])->update($updateData);
    }
}
